﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class DepartmentController : Controller
    {
        private IDepartmentRepository departmentRepository;
        private IUnitOfWork unitOfWork;
        public DepartmentController(IDepartmentRepository departmentRepository, IUnitOfWork unitOfWork)
        {
            this.departmentRepository = departmentRepository;
            this.unitOfWork = unitOfWork;
        }

        //将Department转换为DepartmentModel
        private IEnumerable<DepartmentModel> GetDepartmentModel()
        {
            return from department in departmentRepository.GetAll().AsQueryable()
                   select new DepartmentModel
                              {
                                  ID = department.ID,
                                  Name = department.Name,
                                  Remark = department.Remark
                              };
        }

        public ActionResult Index()
        {
            //首次呈现数据,grid第一次请求是通过服务器端绑定
            return PartialView(GetDepartmentModel());
        }

        [GridAction]
        public ActionResult TelerikAjax()
        {
            //Ajax绑定,排序、分页都会调用该方法
            return View(new GridModel(GetDepartmentModel()));
        }

        [HttpPost]
        [GridAction]
        public ActionResult Insert()
        {
            //创建一个可以编辑的Department对象
            Department department = new Department();

            //执行模型绑定(填充department的属性并且验证他的正确性)
            if (TryUpdateModel(department))
            {
                departmentRepository.Add(department);
            }

            //保存
            unitOfWork.Commit();

            //重新绑定grid
            return View(new GridModel(GetDepartmentModel()));
        }


        [HttpPost]
        [GridAction]
        public ActionResult Update(int id)
        {
            //查找指定Id的department
            Department department = departmentRepository.GetById(id);
            if (department != null)
            {
                //执行模型绑定(填充department对象属性,并进行验证)
                if (TryUpdateModel(department))
                {
                    //模型department验证并填充成功
                    departmentRepository.Update(department);
                }
            }

            //保存
            unitOfWork.Commit();

            //重新绑定grid
            return View(new GridModel(GetDepartmentModel()));
        }

        [HttpPost]
        [GridAction]
        public ActionResult Delete(int id)
        {
            Department department = departmentRepository.GetById(id);

            if (department != null)
            {
                departmentRepository.Delete(department);
            }

            //保存
            unitOfWork.Commit();

            //重新绑定grid
            return View(new GridModel(GetDepartmentModel()));
        }
    }
}
